#!/usr/bin/perl


# 从 /proc/stat 中获得CPU信息，在不同的Linux版本中格式不同。
# Linux lbssvn 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
# cpu  885328 2878 614690 1158097132 37500 2562 3513 0 0

# Linux testharness 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
# cpu  39603271 16204 16424186 4698974809 238084 0 407772 0 0 0

# Linux CPU and Memory
#             total       used       free     shared    buffers     cached
#Mem:        500472     459300      41172          0     131104     157476
#-/+ buffers/cache:     170720     329752
#Swap:            0          0          0

#cat /proc/stat
#cpu  6314920 27721 2039492 1184983360 2360843 4550 92023 0 0
#       user  nice   system   idle      iowait irq  softirq
#cpu0 6314920 27721 2039492 1184983360 2360843 4550 92023 0 0
#intr 1730492045 184 7 0 0 0 0 0 0 0 0 0 0 104 0 0 0 0 0 0 0 0 0 0 22 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8701729 667533 10608228 436
#ctxt 3628056442
#btime 1479994382
#processes 743964
#procs_running 1
#procs_blocked 0
#softirq 1113713278 0 541561557 4905362 4983191 0 0 10958793 0 1969113 549335262
#
#   第一行的数值表示的是CPU总的使用情况，所以我们只要用第一行的数字计算就可以了。下表解析第一行各数值的含义： 解析（单位：jiffies）
# (jiffies是内核中的一个全局变量，用来记录自系统启动一来产生的节拍数，在linux中，一个节拍大致可理解为操作系统进程调度的最小时间片，不同linux内核可能值有不同，通常在1ms到10ms之间)
# user (6314920)    从系统启动开始累计到当前时刻，处于用户态的运行时间，不包含 nice值为负进程。
# nice (27721)      从系统启动开始累计到当前时刻，nice值为负的进程所占用的CPU时间
# system (2039492)  从系统启动开始累计到当前时刻，处于核心态的运行时间
# idle (1184983360)   从系统启动开始累计到当前时刻，除IO等待时间以外的其它等待时间iowait (12256) 从系统启动开始累计到当前时刻，IO等待时间(since 2.5.41)
# irq (2360843)       从系统启动开始累计到当前时刻，硬中断时间(since 2.6.0-test4)
# softirq (4550)   从系统启动开始累计到当前时刻，软中断时间(since 2.6.0-test4)
# 结论2：CPU时间=user+system+nice+idle+iowait+irq+softirq


# 1. 返回物理内存，2，返回交换分区， 3， 空，4，空，5，CPU使用率。
# print "Memory=459300/500472\nSwap=0/0\n\n\nCPU=1113713278\n";
$hostname=$ARGV[0];
$hostname ne "" or $hostname="localhost";


# caculate the latest 1 second's stat.
($i1,$t1)=getcpu();
sleep 1;
($i2,$t2)=getcpu();

$t=$t2-$t1;
$idle=$i2-$i1;
$cpuusage=int((1-$idle/$t)*10000)/100;

my ($usedmemory,$totalmemory,$usedswap,$totalswap)=getmem();

print "Memory=$usedmemory/$totalmemory,Swap=$usedswap/$totalswap,,,CPU=$cpuusage\/100\n";

############################################
# SUB FUNCTION

## Get CPU infomation.
sub getcpu{
	my $r = ($hostname eq "localhost")? `cat /proc/stat | head -n1`:`/usr/bin/ssh $hostname "cat /proc/stat | head -n1"`;
	# print $r;
	chop($r);
	my @fields=split(/\s+/,$r);
	my $idle=$fields[4];
	my $total=0;
	for(my $i=1;$i<scalar @fields;$i++){
		$total=$total+$fields[$i];
	}
	return($idle,$total);
}

## Get Memory infomation.
sub getmem{
	my @r = ($hostname eq "localhost")? `/usr/bin/free`:`/usr/bin/ssh $hostname "/usr/bin/free"`;
	my $totalmemory=0;
	my $usedmemory=0;
	my $totalswap=0;
	my $usedswap=0;
	for $line(@r){
		if($line=~/^Mem:\s+(\d+)\s+(\d+)/){
			$totalmemory=$1;
			$usedmemory=$2;
		}
		if($line=~/^Swap:\s+(\d+)\s+(\d+)/){
			$totalswap=$1;
			$usedswap=$2;
		}
	
	}
	return ($usedmemory,$totalmemory,$usedswap,$totalswap);
}





